package com.drink.yan.domain;

import com.drink.yan.intf.define.YanOpSourceStatusE;
import com.drink.yan.intf.define.YanUserCheckStatusE;
import com.drink.yan.intf.entity.*;
import com.drink.yan.intf.vo.YanUserDetailVo;
import com.drink.yan.web.rest.req.cmd.*;
import com.github.meixuesong.aggregatepersistence.Versionable;
import com.meta.framework.act.entity.SysUser;
import com.meta.framework.common.utils.SecurityUtils;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

/**
 * 人员信息领域对象
 *
 * @Title:
 * @Package: com.drink.yan.domain.
 * @Author: M.simple
 * @Remark: The modified content
 * @CreateDate: 2023-11-25 12:48
 * @Version: v2.0
 */
@Data
public class YanUserDo extends SysUser implements Versionable {

	private YanUserDetailVo yanUserDetail;

	private YanUserStudent yanUserStudent;

	private YanUserAgency yanUserAgency;

	private YanUserSenior yanUserSenior;

	private YanUserTeacher yanUserTeacher;

	private YanUserCounselor yanUserCounselor;

	@Override
	public int getVersion() {
		return 0;
	}

	public void fillStudentInfo(AddStudentInfoCmd cmd, String initPass) {

		if(Objects.isNull(this.getUserId())){
			this.setNickName(cmd.getUserName());
			this.setUserName(cmd.getMobile());
			this.setSex(cmd.getSex());
			this.setPhonenumber(cmd.getMobile());
			this.setPassword(SecurityUtils.encryptPassword(initPass));
			this.setTenantId(1L);
		}else{
			this.setNickName(cmd.getUserName());
			this.setSex(cmd.getSex());
			cmd.setMobile(this.getPhonenumber());
			cmd.setUserId(this.getUserId());
		}

		Optional.ofNullable(cmd.getUserName()).ifPresent(this::setNickName);

		if(Objects.isNull(this.getYanUserDetail())){

			YanUserDetail userDetail = YanUserDetail.builder()
					.userName(cmd.getUserName())
					.idCardNo(cmd.getIdCardNo())
					.userName(cmd.getUserName())
					.mobile(cmd.getMobile())
					.sex(cmd.getSex())
					.userId(cmd.getUserId())
					.headImg(cmd.getHeadImg())
					.build();
			this.setYanUserDetail(userDetail.transform(YanUserDetailVo.class));

		}else{
			Optional.ofNullable(cmd.getUserName()).ifPresent(this.getYanUserDetail()::setUserName);
			Optional.ofNullable(cmd.getIdCardNo()).ifPresent(this.getYanUserDetail()::setIdCardNo);
			Optional.ofNullable(cmd.getHeadImg()).ifPresent(this.getYanUserDetail()::setHeadImg);
			Optional.ofNullable(cmd.getMobile()).ifPresent(this.getYanUserDetail()::setMobile);
			Optional.ofNullable(cmd.getSex()).ifPresent(this.getYanUserDetail()::setSex);
		}

		if(Objects.isNull(this.getYanUserStudent())){

			YanUserStudent userStudent = YanUserStudent.builder()
					.userId(cmd.getUserId())
					.bachelorSchId(cmd.getBachelorSchId())
					.bachelorSchName(cmd.getBachelorSchName())
					.bachelorSpeId(cmd.getBachelorSpeId())
					.bachelorSpeName(cmd.getBachelorSpeName())
					.targetSchId(cmd.getTargetSchId())
					.targetSchName(cmd.getTargetSchName())
					.targetSpeId(cmd.getTargetSpeId())
					.targetSpeName(cmd.getTargetSpeName())
					.targetYear(cmd.getTargetYear())
					.opSource(cmd.getOpSource())
					.addTeacherTime(cmd.getAddTeacherTime())
					.intentionTime(cmd.getIntentionTime())
					.build();
			this.setYanUserStudent(userStudent);
		}else{
			Optional.ofNullable(cmd.getBachelorSpeId()).ifPresent(this.getYanUserStudent()::setBachelorSpeId);
			Optional.ofNullable(cmd.getBachelorSpeName()).ifPresent(this.getYanUserStudent()::setBachelorSpeName);
			Optional.ofNullable(cmd.getBachelorSchId()).ifPresent(this.getYanUserStudent()::setBachelorSchId);
			Optional.ofNullable(cmd.getBachelorSchName()).ifPresent(this.getYanUserStudent()::setBachelorSchName);
			Optional.ofNullable(cmd.getTargetSchId()).ifPresent(this.getYanUserStudent()::setTargetSchId);
			Optional.ofNullable(cmd.getTargetSchName()).ifPresent(this.getYanUserStudent()::setTargetSchName);
			Optional.ofNullable(cmd.getTargetSpeId()).ifPresent(this.getYanUserStudent()::setTargetSpeId);
			Optional.ofNullable(cmd.getTargetSpeName()).ifPresent(this.getYanUserStudent()::setTargetSpeName);
			Optional.ofNullable(cmd.getTargetYear()).ifPresent(this.getYanUserStudent()::setTargetYear);
			Optional.ofNullable(cmd.getAddTeacherTime()).ifPresent(this.getYanUserStudent()::setAddTeacherTime);
			Optional.ofNullable(cmd.getIntentionTime()).ifPresent(this.getYanUserStudent()::setIntentionTime);


		}
	}

	public void applyAgency(ApplyAgencyInfoCmd cmd, String initPass) {

		if(Objects.isNull(this.getUserId())){
			this.setNickName(cmd.getUserName());
			this.setUserName(cmd.getMobile());
			this.setPhonenumber(cmd.getMobile());
			this.setPassword(SecurityUtils.encryptPassword(initPass));
			this.setTenantId(1L);
		}else{
			this.setNickName(cmd.getUserName());
			cmd.setMobile(this.getPhonenumber());
			cmd.setUserId(this.getUserId());
		}

		Optional.ofNullable(cmd.getUserName()).ifPresent(this::setNickName);


		if(Objects.isNull(this.getYanUserDetail())){

			YanUserDetail userDetail = YanUserDetail.builder()
					.userName(cmd.getUserName())
					.idCardObvImg(cmd.getIdCardObvImg())
					.mobile(cmd.getMobile())
					.createBy(SecurityUtils.getUserId())
					.build();
			this.setYanUserDetail(userDetail.transform(YanUserDetailVo.class));
		}else{
			this.getYanUserDetail().setUserName(cmd.getUserName());
			this.getYanUserDetail().setIdCardObvImg(cmd.getIdCardObvImg());
			this.getYanUserDetail().setUpdateBy(SecurityUtils.getUserId());
		}

		if(Objects.isNull(this.getYanUserAgency())){

			YanUserAgency yanUserAgency = YanUserAgency.builder()
					.userId(cmd.getUserId())
					.currentSchId(cmd.getCurrentSchId())
					.currentSchName(cmd.getCurrentSchName())
					.currentSpeId(cmd.getCurrentSpeId())
					.currentSpeName(cmd.getCurrentSpeName())
					.inSchYear(cmd.getInSchYear())
					.status(YanUserCheckStatusE.checking.getCode())
					.createBy(SecurityUtils.getUserId())
					.opSource(cmd.getOpSource())
					.build();
			this.setYanUserAgency(yanUserAgency);
		}else{

			YanUserAgency yanUserAgency = this.getYanUserAgency();
			Optional.ofNullable(cmd.getCurrentSchId()).ifPresent(this.getYanUserAgency()::setCurrentSchId);
			Optional.ofNullable(cmd.getCurrentSchName()).ifPresent(this.getYanUserAgency()::setCurrentSchName);
			Optional.ofNullable(cmd.getCurrentSpeId()).ifPresent(this.getYanUserAgency()::setCurrentSpeId);
			Optional.ofNullable(cmd.getCurrentSpeName()).ifPresent(this.getYanUserAgency()::setCurrentSpeName);
			Optional.ofNullable(cmd.getInSchYear()).ifPresent(this.getYanUserAgency()::setInSchYear);
			this.getYanUserAgency().setUpdateBy(SecurityUtils.getUserId());

			if(YanUserCheckStatusE.check_fail.getCode().equals(yanUserAgency.getStatus())){
				yanUserAgency.setStatus(YanUserCheckStatusE.checking.getCode());
			}
		}
		if(YanOpSourceStatusE.manage_web.getCode().equals(cmd.getOpSource())){
			this.yanUserAgency.setCheckTime(LocalDateTime.now());
			this.yanUserAgency.setStatus(YanUserCheckStatusE.check_succ.getCode());
		}
	}

	public void applySenior(ApplySeniorInfoCmd cmd, String initPass) {

		if(Objects.isNull(this.getUserId())){
			this.setNickName(cmd.getUserName());
			this.setUserName(cmd.getMobile());
			this.setSex(cmd.getSex());
			this.setPhonenumber(cmd.getMobile());
			this.setPassword(SecurityUtils.encryptPassword(initPass));
			this.setTenantId(1L);
			this.setCreateBy(SecurityUtils.getUserId());
		}else{
			this.setNickName(cmd.getUserName());
			this.setSex(cmd.getSex());
			this.setUpdateBy(SecurityUtils.getUserId());
			cmd.setMobile(this.getPhonenumber());
			cmd.setUserId(this.getUserId());
		}

		Optional.ofNullable(cmd.getUserName()).ifPresent(this::setNickName);

		if(Objects.isNull(this.getYanUserDetail())){

			YanUserDetail userDetail = YanUserDetail.builder()
					.userId(cmd.getUserId())
					.userName(cmd.getUserName())
					.mobile(cmd.getMobile())
					.sex(cmd.getSex())
					.idCardObvImg(cmd.getIdCardObvImg())
					.createBy(SecurityUtils.getUserId())
					.build();
			this.setYanUserDetail(userDetail.transform(YanUserDetailVo.class));
		}else{
			this.getYanUserDetail().setUserName(cmd.getUserName());
			this.getYanUserDetail().setIdCardObvImg(cmd.getIdCardObvImg());
			this.getYanUserDetail().setUpdateBy(SecurityUtils.getUserId());
		}

		if(Objects.isNull(this.getYanUserSenior())){

			YanUserSenior yanUserSenior = YanUserSenior.builder()
					.userId(cmd.getUserId())
					.masterSchId(cmd.getMasterSchId())
					.masterSchName(cmd.getMasterSchName())
					.masterSpeId(cmd.getMasterSpeId())
					.masterSpeName(cmd.getMasterSpeName())
					.certImgList(cmd.getCertImgList())
					.serviceList(cmd.getServiceList())
					.inSchYear(cmd.getInSchYear())
					.recruitType(cmd.getRecruitType())
					.recruitName(cmd.getRecruitName())
					.inSchYear(cmd.getInSchYear())
					.firstScore(cmd.getFirstScore())
					.status(YanUserCheckStatusE.checking.getCode())
					.createBy(SecurityUtils.getUserId())
					.opSource(cmd.getOpSource())
					.build();
			this.setYanUserSenior(yanUserSenior);
		}else{
			YanUserSenior yanUserSenior = this.getYanUserSenior();


			Optional.ofNullable(cmd.getMasterSchId()).ifPresent(this.getYanUserSenior()::setMasterSchId);
			Optional.ofNullable(cmd.getMasterSchName()).ifPresent(this.getYanUserSenior()::setMasterSchName);
			Optional.ofNullable(cmd.getMasterSpeId()).ifPresent(this.getYanUserSenior()::setMasterSpeId);
			Optional.ofNullable(cmd.getMasterSpeName()).ifPresent(this.getYanUserSenior()::setMasterSpeName);
			Optional.ofNullable(cmd.getInSchYear()).ifPresent(this.getYanUserSenior()::setInSchYear);
			Optional.ofNullable(cmd.getFirstScore()).ifPresent(this.getYanUserSenior()::setFirstScore);
			Optional.ofNullable(cmd.getRecruitType()).ifPresent(this.getYanUserSenior()::setRecruitType);
			Optional.ofNullable(cmd.getRecruitName()).ifPresent(this.getYanUserSenior()::setRecruitName);
			Optional.ofNullable(cmd.getCertImgList()).ifPresent(this.getYanUserSenior()::setCertImgList);
			Optional.ofNullable(cmd.getServiceList()).ifPresent(this.getYanUserSenior()::setServiceList);
			this.getYanUserSenior().setUpdateBy(SecurityUtils.getUserId());
			if(YanUserCheckStatusE.check_fail.getCode().equals(yanUserSenior.getStatus())){
				yanUserSenior.setStatus(YanUserCheckStatusE.checking.getCode());
			}
		}

		if(YanOpSourceStatusE.manage_web.getCode().equals(cmd.getOpSource())){
			this.yanUserSenior.setCheckTime(LocalDateTime.now());
			this.yanUserSenior.setStatus(YanUserCheckStatusE.check_succ.getCode());
		}
	}

	public void setMySpecial(SetMySpecialCmd cmd) {

		if(Objects.isNull(this.getYanUserStudent())){

			YanUserStudent userStudent = YanUserStudent.builder()
					.userId(cmd.getUserId())
					.targetSchId(cmd.getSchoolId())
					.targetSchName(cmd.getSchoolName())
					.targetSpeId(cmd.getSpecialCode())
					.targetSpeName(cmd.getSpecialName())
					.build();
			this.setYanUserStudent(userStudent);
		}else{
			Optional.ofNullable(cmd.getSchoolId()).ifPresent(this.getYanUserStudent()::setTargetSchId);
			Optional.ofNullable(cmd.getSchoolName()).ifPresent(this.getYanUserStudent()::setTargetSchName);
			Optional.ofNullable(cmd.getSpecialCode()).ifPresent(this.getYanUserStudent()::setTargetSpeId);
			Optional.ofNullable(cmd.getSpecialName()).ifPresent(this.getYanUserStudent()::setTargetSpeName);

		}

	}

	public void authAgencyStatus(AuthAgencyCmd cmd) {

		if(Objects.isNull(this.getYanUserAgency())){
			return;
		}
		this.yanUserAgency.setCheckTime(LocalDateTime.now());
		this.yanUserAgency.setStatus(cmd.getStatus());
		this.yanUserAgency.setRemark(cmd.getRemark());
		this.yanUserAgency.setUpdateBy(SecurityUtils.getUserId());
	}

	public void authSeniorStatus(AuthAgencyCmd cmd) {

		if(Objects.isNull(this.getYanUserSenior())){
			return;
		}
		this.yanUserSenior.setCheckTime(LocalDateTime.now());
		this.yanUserSenior.setStatus(cmd.getStatus());
		this.yanUserSenior.setRemark(cmd.getRemark());
		this.yanUserSenior.setUpdateBy(SecurityUtils.getUserId());

	}

	public void saveUserDetail(YanUserDetail cmd) {

		if(Objects.isNull(this.getYanUserDetail())){

			YanUserDetail userDetail = YanUserDetail.builder()
					.userName(cmd.getUserName())
					.sex(cmd.getSex())
					.idCardNo(cmd.getIdCardNo())
					.mobile(cmd.getMobile())
					.headImg(cmd.getHeadImg())
					.createBy(SecurityUtils.getUserId())
					.build();
			this.setYanUserDetail(userDetail.transform(YanUserDetailVo.class));
		}else{
			Optional.ofNullable(cmd.getHeadImg()).ifPresent(this.getYanUserDetail()::setHeadImg);
			Optional.ofNullable(cmd.getUserName()).ifPresent(this.getYanUserDetail()::setUserName);
			Optional.ofNullable(cmd.getIdCardNo()).ifPresent(this.getYanUserDetail()::setIdCardNo);
			Optional.ofNullable(cmd.getSex()).ifPresent(this.getYanUserDetail()::setSex);
			Optional.ofNullable(cmd.getMobile()).ifPresent(this.getYanUserDetail()::setMobile);

			this.getYanUserDetail().setUpdateBy(SecurityUtils.getUserId());
		}
	}

	public void applyCounselor(ApplyCounselorInfoCmd cmd, String initPass) {

		if(Objects.isNull(this.getUserId())){
			this.setNickName(cmd.getUserName());
			this.setUserName(cmd.getMobile());
			this.setPhonenumber(cmd.getMobile());
			this.setPassword(SecurityUtils.encryptPassword(initPass));
			this.setTenantId(1L);
			this.setCreateBy(SecurityUtils.getUserId());
		}else{
			this.setNickName(cmd.getUserName());
			this.setUpdateBy(SecurityUtils.getUserId());
			cmd.setMobile(this.getPhonenumber());
			cmd.setUserId(this.getUserId());
		}

		Optional.ofNullable(cmd.getUserName()).ifPresent(this::setNickName);

		if(Objects.isNull(this.getYanUserDetail())){

			YanUserDetail userDetail = YanUserDetail.builder()
					.userName(cmd.getUserName())
					.mobile(cmd.getMobile())
					.idCardNo(cmd.getIdCardNo())
					.sex(cmd.getSex())
					.headImg(cmd.getHeadImg())
					.createBy(SecurityUtils.getUserId())
					.build();
			this.setYanUserDetail(userDetail.transform(YanUserDetailVo.class));
		}else{
			Optional.ofNullable(cmd.getUserName()).ifPresent(this.getYanUserDetail()::setUserName);
			Optional.ofNullable(cmd.getIdCardNo()).ifPresent(this.getYanUserDetail()::setIdCardNo);
			Optional.ofNullable(cmd.getSex()).ifPresent(this.getYanUserDetail()::setSex);
			Optional.ofNullable(cmd.getHeadImg()).ifPresent(this.getYanUserDetail()::setHeadImg);
			Optional.ofNullable(cmd.getMobile()).ifPresent(this.getYanUserDetail()::setMobile);


			this.getYanUserDetail().setUpdateBy(SecurityUtils.getUserId());
		}

		if(Objects.isNull(this.getYanUserCounselor())){

			YanUserCounselor yanUserCounselor = YanUserCounselor.builder()
					.userId(cmd.getUserId())
					.currentSchId(cmd.getCurrentSchId())
					.currentSchName(cmd.getCurrentSchName())
					.currentSpeId(cmd.getCurrentSpeId())
					.currentSpeName(cmd.getCurrentSpeName())
					.inSchYear(cmd.getInSchYear())
					.createBy(SecurityUtils.getUserId())
					.opSource(cmd.getOpSource())
					.build();
			this.setYanUserCounselor(yanUserCounselor);
		}else{

			Optional.ofNullable(cmd.getCurrentSchId()).ifPresent(this.getYanUserCounselor()::setCurrentSchId);
			Optional.ofNullable(cmd.getCurrentSchName()).ifPresent(this.getYanUserCounselor()::setCurrentSchName);
			Optional.ofNullable(cmd.getCurrentSpeId()).ifPresent(this.getYanUserCounselor()::setCurrentSpeId);
			Optional.ofNullable(cmd.getCurrentSpeName()).ifPresent(this.getYanUserCounselor()::setCurrentSpeName);
			Optional.ofNullable(cmd.getInSchYear()).ifPresent(this.getYanUserCounselor()::setInSchYear);
			this.getYanUserCounselor().setUpdateBy(SecurityUtils.getUserId());
		}
	}

	public void applyTeacherInfo(ApplyTeacherInfoCmd cmd, String initPass) {

		if(Objects.isNull(this.getUserId())){
			this.setNickName(cmd.getUserName());
			this.setUserName(cmd.getMobile());
			this.setPhonenumber(cmd.getMobile());
			this.setPassword(SecurityUtils.encryptPassword(initPass));
			this.setTenantId(1L);
			this.setCreateBy(SecurityUtils.getUserId());
		}else{
			//this.setNickName(cmd.getUserName());
			this.setUpdateBy(SecurityUtils.getUserId());
			cmd.setUserName(this.getNickName());
			cmd.setMobile(this.getPhonenumber());
			cmd.setUserId(this.getUserId());
		}

		Optional.ofNullable(cmd.getUserName()).ifPresent(this::setNickName);

		if(Objects.isNull(this.getYanUserDetail())){

			YanUserDetail userDetail = YanUserDetail.builder()
					.userName(cmd.getUserName())
					.mobile(cmd.getMobile())
					.createBy(SecurityUtils.getUserId())
					.linkQrCode(cmd.getLinkQrCode())
					.build();
			this.setYanUserDetail(userDetail.transform(YanUserDetailVo.class));
		}else{
			this.getYanUserDetail().setUserName(cmd.getUserName());
			this.getYanUserDetail().setUpdateBy(SecurityUtils.getUserId());
			this.getYanUserDetail().setLinkQrCode(cmd.getLinkQrCode());
		}

		if(Objects.isNull(this.getYanUserTeacher())){

			YanUserTeacher yanUserTeacher = YanUserTeacher.builder()
					.userId(cmd.getUserId())
					.currentSchs(cmd.getCurrentSchs())
					.currentSpes(cmd.getCurrentSpes())
					.createBy(SecurityUtils.getUserId())
					.opSource(cmd.getOpSource())
					.build();
			this.setYanUserTeacher(yanUserTeacher);
		}else{

			Optional.ofNullable(cmd.getCurrentSchs()).ifPresent(this.getYanUserTeacher()::setCurrentSchs);
			Optional.ofNullable(cmd.getCurrentSpes()).ifPresent(this.getYanUserTeacher()::setCurrentSpes);
			this.getYanUserTeacher().setUpdateBy(SecurityUtils.getUserId());
		}
	}

	public void fillStaffUser(UserDataAuthCmd cmd) {

		if(Objects.isNull(this.getYanUserDetail())){
			YanUserDetail userDetail = YanUserDetail.builder()
					.userName(this.getUserName())
					.mobile(this.getPhonenumber())
					.createBy(SecurityUtils.getUserId())
					.dataAuth(cmd.getDataTypeAuthIos())
					.build();
			this.setYanUserDetail(userDetail.transform(YanUserDetailVo.class));
		}else{
			Optional.ofNullable(cmd.getDataTypeAuthIos()).ifPresent(this.getYanUserDetail()::setDataAuth);
		}
	}
}
